﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace HtmlUtilities.UITest
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        public MainPageViewModel()
        {
            BrowserCommands = UITest.BrowserCommands.GetCommands().AsObservableCollection();
            DocumentCommands = UITest.DocumentCommands.GetCommands().AsObservableCollection();
            WindowCommands = UITest.WindowCommands.GetCommands().AsObservableCollection();
            ElementCommands = UITest.ElementCommands.GetCommands().AsObservableCollection();
            HtmlObjectCommands = UITest.HtmlObjectCommands.GetCommands(value => CommandResult += value).AsObservableCollection();
            ScriptObjectCommands = (new ScriptObjectCommands()).GetCommands().AsObservableCollection();
            PageCommands = UITest.PageCommands.GetCommands().AsObservableCollection();
            ScriptObjectCollectionCommands = UITest.ScriptObjectCollectionCommands.GetCommands().AsObservableCollection();
        }

        public ObservableCollection<Command<string>> BrowserCommands { get; private set; }
        public ObservableCollection<Command<string>> DocumentCommands { get; private set; }
        public ObservableCollection<Command<string>> WindowCommands { get; private set; }
        public ObservableCollection<Command<string>> ElementCommands { get; private set; }
        public ObservableCollection<Command<string>> HtmlObjectCommands { get; private set; }
        public ObservableCollection<Command<string>> ScriptObjectCommands { get; private set; }
        public ObservableCollection<Command<string>> PageCommands { get; private set; }
        public ObservableCollection<Command<string>> ScriptObjectCollectionCommands { get; private set; }

        private string _commandResult = string.Empty;
        public string CommandResult
        {
            get { return _commandResult; }
            private set
            {
                if (_commandResult != value)
                {
                    _commandResult = value;
                    OnPropertyChanged("CommandResult");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            if(PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        public void ExecuteCommand(Command<string> command)
        {
            CommandResult = command != null ? command.GetValue() : "Command Not Found";
        }
    }

    public static class QueryStringExtensions
    {
        public static string QueryJoin(this IEnumerable<KeyValuePair<string, string>> values)
        {
            return string.Join("&", values.Select(value => value.Key + "=" + value.Value).ToArray());
        }
    }

    public static class IEnumerableExtensions
    {
        public static ObservableCollection<T> AsObservableCollection<T>(this IEnumerable<T> list)
        {
            var result = new ObservableCollection<T>();
            foreach (T item in list)
                result.Add(item);
            return result;
        }
    }
}
